package com.mlick.dddk;

import android.app.Application;

import com.litesuits.orm.LiteOrm;
import com.mlick.dddk.entry.LogEntry;
import com.mlick.dddk.utils.DateUtils;
import com.mlick.dddk.utils.SysUtils;

import it.sauronsoftware.cron4j.Scheduler;

/**
 * Created by mlick on 2018-03-22.
 */
public class DKApplication extends Application {

    Scheduler scheduler;

    private static DKApplication dkApplication;

    static LiteOrm liteOrm;


    @Override
    public void onCreate() {
        super.onCreate();

        SysUtils.out("===>项目创建成功");
        dkApplication = this;

        if (liteOrm == null) {
            liteOrm = LiteOrm.newSingleInstance(getApplicationContext(), "dddk.db");
        }

        liteOrm.setDebugged(true); // open the log

        //clearData(LogEntry.class);
        //for (int i = 0; i < 1000; i++) {
        //    SysUtils.out(liteOrm.insert(new LogEntry("SSS" + i, DateUtils.getDateTime())));
        //}

    }


    public static DKApplication getInstance() {
        if (dkApplication == null) {
            dkApplication = new DKApplication();
        }
        return dkApplication;
    }

    public Scheduler getScheduler() {
        if (scheduler == null)
            scheduler = new Scheduler();
        return scheduler;
    }

    public void stop() {
        if (scheduler != null) {
            scheduler.stop();
            scheduler = null;
        }
    }


    public void saveData(Object logEntry) {
        if (liteOrm != null)
            liteOrm.insert(logEntry);
    }


    public void clearData(Class clazz) {
        if (liteOrm != null)
            liteOrm.delete(clazz);
    }


    public void saveLogData(String logEntry) {
        saveData(new LogEntry(logEntry, DateUtils.getDateTime()));
    }

    public void clearLogData() {
        clearData(LogEntry.class);
    }

}
